<template>
	<div class="system-menu-container layout-pd">

			<div class="system-menu-search mb15">
				<el-button size="default" type="success" class="ml10" @click="onOpenEditMenu('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增分类
				</el-button>
        <div class="refresh-btn" @click="getTableData"><Refresh style="color:#999;width:32px;height:32px;" /></div>
			</div>
			<el-table
				:data="state.tableData.data"
				v-loading="state.tableData.loading"
				style="width: 100%"
				row-key="id"
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
			>
				<el-table-column label="分类名称" show-overflow-tooltip>
					<template #default="scope">
						<span class="ml10">{{ scope.row.name }}</span>
					</template>
				</el-table-column>
				<el-table-column label="logo" show-overflow-tooltip>
					<template #default="scope">
						<el-image
                class="logo-image"
                :src="scope.row.logo"
                :zoom-rate="1.2"
                :preview-src-list="[scope.row.logo]"
                preview-teleported
            />
					</template>
				</el-table-column>
        <el-table-column label="icon" show-overflow-tooltip>
          <template #default="scope">
            {{ scope.row.icon }}
          </template>
        </el-table-column>
				<el-table-column label="排序" show-overflow-tooltip width="100">
					<template #default="scope">
						{{ scope.row.sort }}
					</template>
				</el-table-column>
        <el-table-column label="状态" show-overflow-tooltip width="100">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.status">启用</el-tag>
            <el-tag type="info" v-else>禁用</el-tag>
          </template>
        </el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="140">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenEditMenu('add',scope.row)">新增</el-button>
						<el-button size="small" text type="primary" @click="onOpenEditMenu('edit', scope.row)">修改</el-button>
						<el-button size="small" text type="danger"  @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>

		<EditDialog ref="editDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts">
import { defineAsyncComponent, ref, onMounted, reactive } from 'vue';
import { RouteRecordRaw } from 'vue-router';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useGoodsClassifyApi } from "/@/api/shop/goodsclassify";
import { Refresh } from "@element-plus/icons-vue";

// 引入组件
const EditDialog = defineAsyncComponent(() => import('/@/views/shop/GoodsClassify/dialog.vue'));

// 定义变量内容

const editDialogRef = ref();
const state = reactive({
	tableData: {
		data: [] as RouteRecordRaw[],
		loading: true,
	},
});

// 获取路由数据，真实请从接口获取
const getTableData = () => {
	state.tableData.loading = true;
  useGoodsClassifyApi().getAdminGoodsClassify().then(res => {
		state.tableData.data = res.data;
	}).catch(err => {
		console.log(err);
	});

	state.tableData.loading = false;

};

// 打开编辑菜单弹窗
const onOpenEditMenu = (type: string, row?: RouteRecordRaw) => {
	editDialogRef.value.openDialog(type, row);
};
// 删除当前行
const onTabelRowDel = (row: RouteRecordRaw) => {
	ElMessageBox.confirm(`此操作将永久删除分类：${row.name}, 是否继续?`, '提示', {
		confirmButtonText: '删除',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
      useGoodsClassifyApi().delAdminGoodsClassify({id:row.id}).then(res => {
				if(!res.code){
					ElMessage.success('删除成功');
				  getTableData();
				}
			}).catch(err => {
				console.log(err);
			});
		})
		.catch(() => {});
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
<style>
.logo-image img{
  max-height: 28px;
}
.refresh-btn {
  float: right;cursor: pointer;width:32px;height: 32px;transition: 1s;
}
.refresh-btn:hover {
  transform : rotate(180deg);
}
</style>
